//!
/**
 ***********************************************************************************************************
 * <RBLight Implementation>
 ***********************************************************************************************************
 * @file RBLight.cpp
 ***********************************************************************************************************/

/***********************************************************************************************************
 * Precompiled Header
 ***********************************************************************************************************/
#include "RBMainPCH.h"

/***********************************************************************************************************
 * Includes
 ***********************************************************************************************************/
#include "RBLight.h"

#include "RBGeometry.h"

/***********************************************************************************************************
 * Defines
 ***********************************************************************************************************/

/***********************************************************************************************************
 * Classes implementation
 ***********************************************************************************************************/

RB_IMPLEMENT_COMPONENT(RBLight);

/**
 ***********************************************************************************************************
 * Constructor
 ***********************************************************************************************************/
RBLight::RBLight(RBEntityHandle entityHandle, RBTransformComponentHandle xForm)
: RBTSpatialComponent(entityHandle, xForm)
{
}

/**
 ***********************************************************************************************************
 * Destructor
 ***********************************************************************************************************/
RBLight::~RBLight()
{
    // Warn geometries that we don't exist anymore
    while (!m_aGeometries.empty())
    {
        RemoveGeometry(*m_aGeometries.begin());
    }
}

/**************************************************************************************************************************************************************
 * 
 **************************************************************************************************************************************************************/
float3 RBLight::GetDirection() const
{
    float3 out;
    WorldXForm().Rotate.GetCol(2, out);
    out = -out;

    return out;
}

/***********************************************************************************************************
 * 
 ***********************************************************************************************************/
void RBLight::AddGeometry(RBGeometry* pAffectedGeometry)
{
    if (m_aGeometries.find(pAffectedGeometry) == m_aGeometries.end())
    {
        // We don't have the geometry in our list: add it and warn it
        m_aGeometries.insert(pAffectedGeometry);
    }
}

/***********************************************************************************************************
 * 
 ***********************************************************************************************************/
void RBLight::RemoveGeometry(RBGeometry* pAffectedGeometry)
{
    std::set<RBGeometry*>::iterator iterGeom = m_aGeometries.find(pAffectedGeometry);
    if (iterGeom != m_aGeometries.end())
    {
        // We haven't removed the geometry from our list: add it and warn it
        m_aGeometries.erase(iterGeom);
    }
}

/***********************************************************************************************************
 *
 ***********************************************************************************************************/
void RBLight::CopyMembers(RBLightHandle handleDest)
{
    RBLight* pDst = &Get(handleDest);

    pDst->m_eLightType = m_eLightType;
    pDst->m_kColor = m_kColor;
    pDst->m_fMultiplier = m_fMultiplier;
    pDst->m_fConstantAttenuation = m_fConstantAttenuation;
    pDst->m_fLinearAttenuation = m_fLinearAttenuation;
    pDst->m_fQuadraticAttenuation = m_fQuadraticAttenuation;
    pDst->m_fPenumbraAngle = m_fPenumbraAngle;
    pDst->m_fUmbraAngle = m_fUmbraAngle;
    pDst->m_fFalloff = m_fFalloff;
}

/***********************************************************************************************************
 * 
 ***********************************************************************************************************/
void RBLight::Serialize(RBStream& kStream)
{
    kStream & m_kColor;
    kStream & m_eLightType & m_fMultiplier;
    kStream & m_fConstantAttenuation & m_fLinearAttenuation & m_fQuadraticAttenuation;
    kStream & m_fPenumbraAngle & m_fUmbraAngle;
    kStream & m_fFalloff & m_fFalloff;
}

/***********************************************************************************************************/
